home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8437 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.2 KB  |  211 lines

  1. Path: lince.lander.es!news
  2. From: Leonardo <leonardo@lander.es>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Why C++ sucks++
  5. Date: 18 Feb 1996 11:45:23 GMT
  6. Organization: Bla bla bla
  7. Message-ID: <4g73gj$6cc@lince.lander.es>
  8. References: <1996Feb17.181435.1@willow>
  9. NNTP-Posting-Host: ppp032.lander.es
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
  14.  
  15. ferriom@woods.uml.edu wrote:
  16. >
  17. >
  18. >*** HIGH SCHOOL ***
  19. >
  20. >100  PRINT "Hello World."
  21. >999  END
  22. >
  23. >
  24. >
  25. >*** COLLEGE FRESHMAN ***
  26. >
  27. >       WRITE (6, 100)
  28. >  100  FORMAT (1X, 'Hello World.')
  29. >       END
  30. >
  31. >
  32. >
  33. >*** ADVANCED COLLEGE ***
  34. >
  35. >PROGRAM P1(INPUT, OUTPUT);
  36. >
  37. >BEGIN
  38. >WRITELN(OUTPUT, 'Hello World.');
  39. >END.
  40. >
  41. >
  42. >
  43. >*** RECENT COLLEGE GRADUATE ***
  44. >
  45. >       IDENTIFICATION DIVISION.
  46. >       PROGRAM-ID.
  47. >           P1.
  48. >
  49. >       ENVIRONMENT DIVISION.
  50. >
  51. >       DATA DIVISION.
  52. >       WORKING-STORAGE SECTION.
  53. >       01 MESSAGE-RECORD                     PICTURE X(12).
  54. >
  55. >       PROCEDURE DIVISION.
  56. >       PROGRAM-BEGIN.
  57. >       FIRST-PARAGRAPH.
  58. >           MOVE 'Hello World.' TO MESSAGE-RECORD.
  59. >           DISPLAY MESSAGE-RECORD.
  60. >       PROGRAM-DONE.
  61. >           STOP RUN.
  62. >
  63. >
  64. >
  65. >*** SEASONED PROFESSIONAL ***
  66. >
  67. >#include <iostream.h>
  68. >#include <string.h>
  69. >
  70. >
  71. >class String
  72. >{
  73. > public:
  74. >  String();
  75. >  String(const char *const);
  76. >  String(const String &);
  77. >  ~String();
  78. >  char & operator[](unsigned short offset);
  79. >  char operator[](unsigned short offset) const;
  80. >  String operator+(const String&);
  81. >  void operator+=(const String&);
  82. >  String & operator= (const String &);
  83. >  unsigned short GetLen()const {return itsLen; }
  84. >  const char * GetString() const {return itsString; }
  85. > private:
  86. >  String (unsigned short);
  87. >  char * itsString;
  88. >  unsigned short itsLen;
  89. >};
  90. >
  91. >
  92. >String::String()
  93. >{
  94. > itsString = new char[1];
  95. > itsString[0] = '\0';
  96. > itsLen = 0;
  97. >}
  98. >
  99. >String::String(unsigned short len)
  100. >{
  101. > itsString = new char[len + 1];
  102. > for (unsigned short i = 0;  i <= len;  i++)
  103. >  itsString[i] = '\0';
  104. > itsLen = len;
  105. >}
  106. >
  107. >String::String(const char * const cString)
  108. >{
  109. > itsLen = strlen(cString);
  110. > itsString = new char[itsLen + 1];
  111. > for (unsigned short i = 0;  i < itsLen;  i++)
  112. >  itsString[i] = cString[i];
  113. > itsString[itsLen] = '\0';
  114. >}
  115. >
  116. >String::String(const String & rhs)
  117. >{
  118. > itsLen = rhs.GetLen();
  119. > itsString = new char[itsLen + 1];
  120. > for (unsigned short i = 0;  i < itsLen;  i++)
  121. >  itsString[i] = rhs[i];
  122. > itsString[itsLen] = '\0';
  123. >}
  124. >
  125. >String::~String()
  126. >{
  127. > delete [] itsString;
  128. > itsLen = 0;
  129. >}
  130. >
  131. >String& String::operator=(const String & rhs)
  132. >{
  133. > if (this == &rhs)
  134. >  return *this;
  135. > delete [] itsString;
  136. > itsLen = rhs.GetLen();
  137. > itsString = new char[itsLen + 1];
  138. > for (unsigned short i = 0;  i < itsLen;  i++)
  139. >  itsString[i] = rhs[i];
  140. > itsString[itsLen] = '\0';
  141. > return *this;
  142. >}
  143. >
  144. >char & String::operator[](unsigned short offset)
  145. >{
  146. > if (offset > itsLen)
  147. >  return itsString[itsLen - 1];
  148. > else
  149. > return itsString[offset];
  150. >}
  151. >
  152. >char String::operator[](unsigned short offset) const
  153. >{
  154. > if (offset > itsLen)
  155. >  return itsString[itsLen - 1];
  156. > else
  157. >  return itsString[offset];
  158. >}
  159. >
  160. >String String::operator+(const String& rhs)
  161. >{
  162. > unsigned short totalLen = itsLen + rhs.GetLen();
  163. > String temp(totalLen);
  164. > for (unsigned short i = 0;  i < itsLen;  i++)
  165. >  temp[i] = itsString[i];
  166. > for (unsigned short j = 0;  j < rhs.GetLen();  j++, i++)
  167. >  temp[i] = rhs[j];
  168. > temp[totalLen] = '\0';
  169. > return temp;
  170. >}
  171. >
  172. >void String::operator+=(const String& rhs)
  173. >{
  174. > unsigned short rhsLen = rhs.GetLen();
  175. > unsigned short totalLen = itsLen + rhsLen;
  176. > String temp(totalLen);
  177. > for (unsigned short i = 0;  i < itsLen;  i++)
  178. >  temp[i] = itsString[i];
  179. > for (unsigned short j = 0;  j < rhs.GetLen();  j++,  i++)
  180. >  temp[i] = rhs[i - itsLen];
  181. > temp[totalLen] = '\0';
  182. > *this = temp; 
  183. >}
  184. >
  185. >
  186. >main()
  187. >{
  188. > String s1("Hello");
  189. > String s2(" ");
  190. > String s3("World.");
  191. > String s4 = s1 + s2 + s3;
  192. > cout << s4.GetString() << endl;
  193. >}
  194.  
  195. Hey pal, I don't know why I even bother.
  196. The code comparable to that written in all other languages would be 
  197. something like this:
  198.  
  199. #include <iostream.h>
  200.  
  201. main()
  202. {
  203. cout << "Hello fucken world" << endl;
  204. }
  205.  
  206. And the method is much more type-safe not only for strings but for all 
  207. other types that you create.
  208. Bye
  209.  
  210.  
  211.